package com.kaoshi.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.agency.domain.Qianyue;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.Gl;
import com.kaoshi.entity.Jkjl;
import com.kaoshi.entity.Person;
//import com.kaoshi.entity.Qianyue;
import com.kaoshi.entity.*;

//import com.kaoshi.service.IPersonService;
import com.kaoshi.management.pojo.XtManagement;
import com.kaoshi.service.IPersonService;
import org.joda.time.DateTime;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-25
 */
@RestController
@CrossOrigin
@RequestMapping("/person")
public class PersonController {

    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.keyId}")
    private String keyid;
    @Value("${aliyun.keySecret}")
    private String keysecret;
    @Value("${aliyun.bucketName}")
    private String buncket;
    @Autowired
    private IPersonService personService;


    @RequestMapping("findList")
    public Result findList(Integer current, Integer pageSize, @RequestBody Person person){
        Page<Person> page=personService.findList(current,pageSize,person);
        return Result.ok(page);
    }

    @RequestMapping("hqgl")
    public Result hqgl(){
        List<Person> peopl=personService.hqgl(0);
        return Result.ok(peopl);
    }

    @RequestMapping("mhList")
    public Result mhList(@RequestBody Gl gl){

        List<Person> personList=personService.mhList(gl);
        return Result.ok(personList);
    }

    @RequestMapping("yhgl")
    public Result yhgl(@RequestBody Gl gl){
        List<Person> personList=personService.yhgl(gl);
        return Result.ok(personList);
    }

    @RequestMapping("addJm")
    public Result addJm(@RequestBody Person person){
        person.setLj(0);
        person.setQystatus(2);
        personService.addJm(person);
        return Result.ok();
    }

    @RequestMapping("getInfo")
    public Result getInfo(Integer id){
        Person person=personService.getById(id);
        System.out.println(person);
        return Result.ok(person);
    }

    @RequestMapping("upload")
    public String fileUpload(MultipartFile file) {
        String url="";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        try {
            // 创建PutObjectRequest对象。
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();

            String replace = UUID.randomUUID().toString().replace("-", "");
            originalFilename = replace+originalFilename;
            String time = new DateTime().toString("yyyy/MM/dd");
            originalFilename = time+"/"+originalFilename;

            PutObjectResult putObjectResult = ossClient.putObject(buncket, originalFilename, inputStream);

            url="https://"+buncket+"."+endpoint+"/"+originalFilename;
        } catch (Exception ce) {
            System.out.println("上传异常--");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }


    @RequestMapping("getById")
    public Result getById(@RequestBody Gl gl){
        List<Person> personList=personService.getByPid(gl);
        return Result.ok(personList);
    }


    @RequestMapping("update")
    public Result update(@RequestBody Person person){
//
        Integer id=personService.updatee(person);
        return Result.ok(id);
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        personService.delete(id);
        return Result.ok();
    }

    @RequestMapping("outExcel")
    public void outExcel(HttpServletResponse response) throws IOException {
        //1.设置响应数据
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生列表", "UTF-8").replaceAll("/+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //2.查询要导出的数据
        List<Person> studentList= personService.list();

        //处理图片的传输问题
        for (Person institution : studentList) {

            String introduce = institution.getImg();
//去除图片中数据的前缀
            String replace = introduce.replace("https://spyx-yy.oss-cn-beijing.aliyuncs.com/", "");
//3.更改oss桶
            byte[] imgBytes = OssImageUtils.getImageBytesFromOss("spyx-yy", replace);
            institution.setImgBytes(imgBytes);

        }
        //3.使用EasyExcel完成导出
        EasyExcel.write(response.getOutputStream())
                .head(Person.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("学生列表")
                .doWrite(studentList);
    }

    @RequestMapping("qy")
    public Result qy(Integer id){
        List<Qianyue> qianyueList=personService.qy(id);
        return Result.ok(qianyueList);
    }

    @RequestMapping("updateStatus")
    public Result updateStatus(@RequestBody Person person){
        personService.updateById(person);
        return Result.ok();
    }

    @RequestMapping("getAll")
    public Result getAll(){
        List<Person> personList=personService.list();
        return Result.ok(personList);
    }


    @RequestMapping("team")
    public Result team(){
        List<Team> teams=personService.team();
        return Result.ok(teams);
    }


    @RequestMapping("bao")
    public Result bao(){
        List<Bao> baos=personService.bao();
        return Result.ok(baos);
    }

    @RequestMapping("jg")
    public Result jg(){
        List<Institution> institutions=personService.jg();
        return Result.ok(institutions);
    }


    @GetMapping("findCount")
    public List<Map<String, Object>> line() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Person> personList = personService.findAll();
        for (Person person : personList) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", person.getNum());
            map.put("name", person.getWhcd());
            list.add(map);
        }
        return list;
    }

    @GetMapping("findCount1")
    public List<Map<String, Object>> bar() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Person> personList = personService.findAll1();
        for (Person person : personList) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", person.getNum());
            map.put("name", person.getSex());
            list.add(map);
        }
        return list;
    }


    @RequestMapping("getBq")
    public Result getBq(){
        List<XtManagement> bqs=personService.getBq();
        return Result.ok(bqs);
    }

}
